﻿using MediatR;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.Farmings_NextWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.Farmings_NextWay
{
    public class UpdateNextWayCommandHandler : IRequestHandler<UpdateNextWayCommand, ApiResult<int>>
    {
        IBaseReponsitory<Farmings_Next> Farmings_Next;

        public UpdateNextWayCommandHandler(IBaseReponsitory<Farmings_Next> Farmings_Next)
        {
            this.Farmings_Next = Farmings_Next;
        }

        public async Task<ApiResult<int>> Handle(UpdateNextWayCommand request, CancellationToken cancellationToken)
        {
            if (request.Farmings_Next.Farming_Please == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "养殖场不能为空",
                    Data = -1
                };
            }
            if (request.Farmings_Next.Farming_People == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "负责人不能为空",
                    Data = -1
                };
            }
            if (request.Farmings_Next.Farming_SaveCout == 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "保存量不能为0",
                    Data = -1
                };
            }

            await Farmings_Next.UpdateAsync(request.Farmings_Next);
            return new ApiResult<int>
            {
                Code = ApiEnum.Success,
                Message = "修改成功",
                Data = 1
            };
        }
    }
}
